//
//  GHJokeTableViewCell.m
//  VideoPic
//
//  Created by GH on 16/3/23.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "GHJokeTableViewCell.h"
#import "GHModelOfJoke.h"
#import <YYWebImage.h>

@interface GHJokeTableViewCell ()
@property (nonatomic ,retain)UILabel *labelOfUserName;
@property (nonatomic ,retain)UILabel *labelContect;
@property (nonatomic ,retain)UILabel * labelOfCap;
@property (nonatomic ,retain)UIImageView *imageOfHead;
@property (nonatomic ,retain)UILabel *userName;
@property (nonatomic ,retain)UIImageView *userImage;
@property (nonatomic ,retain)UILabel *labelOfUserContect;
@property (nonatomic ,retain)UIButton *buttonOfgood;
@property (nonatomic ,retain)UILabel *labelOfgoodCount;
@property (nonatomic ,retain)UIButton *buttonWithDZ;
@property (nonatomic ,retain)UIButton *buttonWithdigg;
@property (nonatomic ,retain)UIButton *buttonComm;
@property (nonatomic ,retain)UIButton *buttonShare;
@property (nonatomic ,retain)UILabel *labelWithDZ;
@property (nonatomic ,retain)UILabel *labelWithdigg;
@property (nonatomic ,retain)UILabel *labelOfComm;
@property (nonatomic ,retain)UILabel *labelOfshare;
@property (nonatomic ,retain)NSString *strId;


@end

@implementation GHJokeTableViewCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {

    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createImage];
        [self createLabel];
        [self clickButton];
        
    }
    return self;
}


- (void)clickButton {
   self.buttonOfgood = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.contentView addSubview:self.buttonOfgood];
    
    self.buttonWithDZ = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.contentView addSubview:self.buttonWithDZ];
    
    self.buttonWithdigg = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.contentView addSubview:self.buttonWithdigg];
    
    self.buttonComm = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.contentView addSubview:self.buttonComm];
    
    self.buttonShare = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.contentView addSubview:self.buttonShare];
    
}


- (void)createLabel {
    
    self.labelOfUserName = [[UILabel alloc] init];
    [self.contentView addSubview:self.labelOfUserName];
 
    self.labelContect = [[UILabel alloc] init];
    [self.contentView addSubview:self.labelContect];
    self.labelContect.numberOfLines = 0;
    
    self.labelOfCap = [[UILabel alloc] init];
    [self.contentView addSubview:self.labelOfCap];
    self.labelOfCap.font = [UIFont systemFontOfSize:12];
    
    self.userName = [[UILabel alloc] init];
    [self.contentView addSubview:self.userName];
    
    self.labelOfUserContect = [[UILabel alloc] init];
    [self.contentView addSubview:self.labelOfUserContect];
    
    self.labelOfgoodCount = [[UILabel alloc] init];
    [self.contentView addSubview:self.labelOfgoodCount];
    
    self.labelWithDZ = [[UILabel alloc] init];
    [self.contentView addSubview:self.labelWithDZ];
    
    self.labelWithdigg = [[UILabel alloc] init];
    [self.contentView addSubview:self.labelWithdigg];
    
    self.labelOfComm = [[UILabel alloc] init];
    [self.contentView addSubview:self.labelOfComm];
    
    self.labelOfshare = [[UILabel alloc] init];
    [self.contentView addSubview:self.labelOfshare];
    

    
}

- (void)passWithModel:(GHModelOfJoke *)model {

    self.labelOfUserName.text = model.name;
    self.imageOfHead.yy_imageURL = [NSURL URLWithString:model.avatar_url];
    self.labelContect.text = model.content;
    self.labelContect.numberOfLines = 0;
    self.labelOfCap.text = model.category_name;


    self.userName.text = model.user_name;
    self.userImage.yy_imageURL = [NSURL URLWithString:model.user_profile_image_url];

    self.labelOfUserContect.text = model.text;
    
    //点赞
    self.labelOfgoodCount.text = model.comment_count.description;
    
    
    self.labelWithDZ.text = model.digg_count.description;
    self.labelWithdigg.text = model.bury_count.description;
    

    self.labelOfComm.text = model.comm.description;
    
    self.labelOfshare.text = model.share_count.description;

}


-(CGFloat)heightForLabel:(NSString *)text{
    
    CGRect rect = [text boundingRectWithSize:CGSizeMake(SCREEN_WIDTH - 40, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:17]} context:nil];
    
    
    return rect.size.height;
}


- (void)createImage {
    
    self.imageOfHead = [[UIImageView alloc] init];
    [self.contentView addSubview:self.imageOfHead];
    
    self.userImage = [[UIImageView alloc] init];
    [self.contentView addSubview:self.userImage];
    ;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    //头像
    self.imageOfHead.frame = CGRectMake(20, 20, SCREEN_WIDTH / 10, SCREEN_WIDTH / 10);
    self.imageOfHead.layer.masksToBounds = YES;
    self.imageOfHead.layer.cornerRadius = SCREEN_WIDTH / 20;
    
    //用户名
    self.labelOfUserName.frame = CGRectMake(30 + SCREEN_WIDTH / 10, 20 + SCREEN_WIDTH/ 20 - 13, SCREEN_WIDTH/2, 25);
    self.labelOfUserName.font = [UIFont systemFontOfSize:13];
    
    //title
    self.labelContect.frame = CGRectMake(20, 20 + SCREEN_WIDTH /10 +15, SCREEN_WIDTH  - 40, [self heightForLabel:self.labelContect.text]);
    CGFloat titleBom = [self heightForLabel:self.labelContect.text] + 35 + SCREEN_WIDTH / 10;
    
    CGFloat userHeight;
    
    //cap
    self.labelOfCap.numberOfLines = 0;
    if (self.labelContect == nil) {
        self.labelOfCap.frame = CGRectMake(20, titleBom + 15 - 20, [self heightForLabel:self.labelOfCap.text], 20);
        userHeight = titleBom + 15;
    }else {
        self.labelOfCap.frame = CGRectMake(20, titleBom + 15, 100, 20);
        userHeight = titleBom + 15;
    }
    self.labelOfCap.textAlignment = 1;
    self.labelOfCap.layer.borderWidth = 1;
    self.labelOfCap.layer.borderColor = [UIColor blackColor].CGColor;
    self.labelOfCap.layer.masksToBounds = YES;
    self.labelOfCap.layer.cornerRadius = 10;
    
    
    
    self.buttonOfgood.hidden = YES;
    if (self.labelOfUserContect.text) {

    //userimage
        self.userImage.frame = CGRectMake(20, userHeight + 40, SCREEN_WIDTH /10, SCREEN_WIDTH / 10);
        self.userImage.layer.masksToBounds = YES;
        self.userImage.layer.cornerRadius = SCREEN_WIDTH / 20;
        
    self.userName.frame = CGRectMake(30 + SCREEN_WIDTH / 10,userHeight + 40 + SCREEN_WIDTH/ 20 - 13, SCREEN_WIDTH /2, 25);
        self.userName.font = [UIFont systemFontOfSize:13];
    self.userName.textColor = [UIColor grayColor];
        
        self.labelOfUserContect.frame = CGRectMake(20, userHeight + 80, SCREEN_WIDTH - 40, [self heightForLabel:self.labelOfUserContect.text]);
        self.labelOfUserContect.numberOfLines = 0;
        self.labelOfUserContect.font = [UIFont systemFontOfSize:15];
        self.labelOfUserContect.textColor = [UIColor grayColor];

        self.buttonOfgood.frame = CGRectMake(SCREEN_WIDTH - 90,userHeight + 40 + SCREEN_WIDTH/ 20 - 13, 30, 30);
        
        [self.buttonOfgood setImage:[UIImage imageNamed:@"good"] forState:UIControlStateNormal];
    self.buttonOfgood.hidden = NO;
    
    self.labelOfgoodCount.frame = CGRectMake(SCREEN_WIDTH - 60, userHeight + 40 + SCREEN_WIDTH/ 20 - 13, 50, 30);
    self.labelOfgoodCount.font = [UIFont systemFontOfSize:13];
    self.labelOfgoodCount.textColor = [UIColor grayColor];
        userHeight = userHeight + 60 + [self heightForLabel:self.labelOfUserContect.text];
    }
    
    self.buttonWithDZ.frame = CGRectMake(20, userHeight + 35, 20, 20);
    [self.buttonWithDZ setImage:[UIImage imageNamed:@"zan"] forState:UIControlStateNormal];
    
    self.buttonWithdigg.frame = CGRectMake(SCREEN_WIDTH / 3 - 10, userHeight + 35, 20, 20);
    [self.buttonWithdigg setImage:[UIImage imageNamed:@"bad"] forState:UIControlStateNormal];
    
    
    //buttonCom
    self.buttonComm.frame = CGRectMake(SCREEN_WIDTH /3 *2 - 40, userHeight + 35, 20, 20);
    [self.buttonComm setImage:[UIImage imageNamed:@"pinglun"] forState:UIControlStateNormal];
    
    //分享
    self.buttonShare.frame = CGRectMake(SCREEN_WIDTH - 80, userHeight +35, 20, 20);
    [self.buttonShare setImage:[UIImage imageNamed:@"share"] forState:UIControlStateNormal];
    
    //点赞
    self.labelWithDZ.frame = CGRectMake(45, userHeight + 35, 45, 20);
    self.labelWithDZ.font = [UIFont systemFontOfSize:13];
    self.labelWithDZ.textColor = [UIColor grayColor];
    
    
    self.labelWithdigg.frame = CGRectMake(SCREEN_WIDTH / 3- 10 + 20 + 10, userHeight + 35, 40, 20);
    self.labelWithdigg.font = [UIFont systemFontOfSize:13];
    self.labelWithdigg.textColor = [UIColor grayColor];
    
    self.labelOfComm.frame = CGRectMake(SCREEN_WIDTH /3 *2 - 40 + 30, userHeight +35, 50, 20);
    self.labelOfComm.font = [UIFont systemFontOfSize:13];
    self.labelOfComm.textColor = [UIColor grayColor];
    
    self.labelOfshare.frame = CGRectMake(SCREEN_WIDTH -50, userHeight + 35, 40, 20);
    self.labelOfshare.font = [UIFont systemFontOfSize:13];
    self.labelOfshare.textColor = [UIColor grayColor];
    
}

+ (CGFloat)heightForCellWith:(GHModelOfJoke *)model {

    CGFloat height;

    if (model.text == nil) {
        
        height  = [self heightForLabel:model.content]  + [UIScreen mainScreen].bounds.size.width / 10 + 60 + 65;
    }else {
    
        height = [self heightForLabel:model.content]  + [UIScreen mainScreen].bounds.size.width / 10 + 60 + [self heightForLabel:model.text] + 60 + 65 ;
    
    }
    

    return height;
}


+(CGFloat)heightForLabel:(NSString *)text{
    
    CGRect rect = [text boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width - 40, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:17]} context:nil];
    
    
    return rect.size.height;
}



- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
